package com.example.springboot.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.controller.dto.ArticleDTO;
import com.example.springboot.entity.Articles;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 文章表 Mapper 接口
 * </p>
 *
 * @author
 * @since
 */
public interface ArticlesMapper extends BaseMapper<Articles> {

    void updateLike(Integer id);

    void updateUnLike(Integer id);

    /**
     * 分页查询文章列表（关联用户表获取作者信息）
     */
    @Select("SELECT a.*, u.nickName AS nickName, u.avatar_url AS avatar " +
            "FROM articles a LEFT JOIN sys_user u ON a.author_id = u.id " +
            "${ew.customSqlSegment}") // 支持 MyBatis-Plus 条件构造器
    Page<ArticleDTO>
    selectArticlePageWithAuthor(Page<ArticleDTO> page, @Param(Constants.WRAPPER) Wrapper<Articles> wrapper);


    void updateView(Integer categoryId);
    // 在 ArticlesMapper 接口中
    @Select("SELECT COUNT(1) FROM likes WHERE article_id = #{articleId} AND user_id = #{userId}")
    Integer selectisLikes(@Param("articleId") Integer articleId, @Param("userId") Integer userId);
    @Delete("DELETE FROM likes WHERE article_id = #{articleId} AND user_id = #{userId}")
    void deleteisLikesById(@Param("articleId") Integer articleId, @Param("userId") Integer userId);
    @Select("SELECT * FROM articles WHERE status = 1")
    List<Articles> selectlist();
}
